/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires.entites;

import fr.ipst.spot.entites.AbstractSpotEntite;
import fr.ipst.spot.exceptions.SpotExceptionForm;
import fr.ipst.spot.formulaires.FormAbstractHtml;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @param <T>
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public abstract class FormAbstractHtmlEntite<T extends AbstractSpotEntite> extends FormAbstractHtml {

    /**
     *
     */
    protected FormAbstractHtmlEntiteId<T> formId;

    /** 
     *
     * @param formId
     * @param request
     */
    public FormAbstractHtmlEntite(FormAbstractHtmlEntiteId<T> formId, HttpServletRequest request) {
        this.formId = formId;
        this.msgErreurValidate = "";
    }

    /**
     *
     * @param formId
     * @param entity
     */
    public FormAbstractHtmlEntite(FormAbstractHtmlEntiteId<T> formId, T entity) {
        this.formId = formId;
        this.msgErreurValidate = "";
    }

    /**
     *
     * @return
     */
    public Integer getId() {
        return this.formId.getId();
    }

    /**
     *
     * @throws SpotExceptionForm
     */
    public void validateCreation() throws SpotExceptionForm {
        this.validate(true);
    }

    /**
     *
     * @throws SpotExceptionForm
     */
    public void validateModification() throws SpotExceptionForm {
        this.validate(false);
    }

    /**
     *
     * @param creation
     * @throws SpotExceptionForm
     */
    protected abstract void validate(boolean creation) throws SpotExceptionForm;

    /**
     *
     * @return @throws SpotExceptionForm
     * @throws SpotExceptionForm
     */
    public abstract T getEntityToPersist() throws SpotExceptionForm;

    /**
     *
     * @param entityToUpdate
     * @return
     * @throws SpotExceptionForm
     */
    public T getEntityToUpdate(T entityToUpdate) throws SpotExceptionForm {
        return this.garnir(entityToUpdate, false);
    }

    /**
     *
     * @param entityToFeed
     * @param creation
     * @return
     * @throws SpotExceptionForm
     */
    protected abstract T garnir(T entityToFeed, boolean creation) throws SpotExceptionForm;
}
